﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

using Data;

namespace RCFlightTimer
{
	public class RCObjectAdapter : BaseAdapter<RCObject>
	{
		RCObject[] items;
		Activity context;

		public RCObjectAdapter(Activity context, RCObject[] items) : base() {
			this.context = context;
			this.items = items;
		}
		public override long GetItemId(int position)
		{
			return position;
		}

		public override RCObject this[int position] {  
			get { return items[position]; }
		}

		public override int Count {
			get { return items.Length; }
		}

		public override View GetView(int position, View convertView, ViewGroup parent)
		{
			View view = convertView; // re-use an existing view, if one is supplied
			if (view == null) // otherwise create a new one
				view = context.LayoutInflater.Inflate(Android.Resource.Layout.SimpleSpinnerItem, null);
			// set view properties to reflect data for the given row
			view.FindViewById<TextView>(Android.Resource.Id.Text1).Text = items[position].name;
			// return the view, populated with data, for display
			return view;
		}

		/*
		 * Creates and returns an RCObjectAdapter containing all of the aircraft in the database and populates items with the aircraft
		 * */
		public static RCObjectAdapter getAircraftAdapter(Activity context, out RCObject[] items){
			items = Database.Instance.GetAllAircraft ().ToArray ();

			return new RCObjectAdapter (context, items);
		}
	}
}

